package domain.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import domain.data.QnAPersistenceManager;
import domain.model.Whois;

@SuppressWarnings("serial")
public class SearchWhoisServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        String ip = req.getRemoteAddr();

        req.setAttribute("ip", ip);        
        PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
        
        String domain = req.getParameter("name");
        domain = domain.toLowerCase().replaceAll("(http|https)+://(www.)*", "").replaceAll("^[\\w\\W]+[\\/]{2}", "").replaceAll("[\\/]+[\\w\\W]+$", "").replaceAll("www.", "");
        
        Query query = psm.newQuery(Whois.class);
        query.setFilter("name=='"+domain+"'");
        @SuppressWarnings("unchecked")
		List<Whois> listWhois = (List<Whois>) query.execute();
        
        if(listWhois.size() > 0)
        {
	        req.setAttribute("listWhois", listWhois.get(0));
	        
	        Cache cache=null;

	        try {
	            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
	        } catch (CacheException e) {
	           e.printStackTrace();
	           
	        }
	        if(cache.containsKey("lastPingWhois"))
	        {
	        	@SuppressWarnings("unchecked")
				ArrayList<String> lastPing = (ArrayList<String>)cache.get("lastPingWhois");
	        	if(lastPing.size()>19)
	        	{
	        		lastPing.remove(0);
	        		
	        		
	        	}
	        	if(!lastPing.contains(listWhois.get(0).getName()))
	        	lastPing.add(listWhois.get(0).getName());
	        	cache.put("lastPingWhois", lastPing);
	        	
	        }
	        else
	        {
	        	
	        	ArrayList<String> lastPing = new ArrayList<String>();
	        	if(!lastPing.contains(listWhois.get(0).getName()))
	        	lastPing.add(listWhois.get(0).getName());
	        	cache.put("lastPingWhois", lastPing);
	        }
	        
	        req.setAttribute("title", "Whois lookup : Domain "+domain+" - domain-infomation.appspot.com");
			req.setAttribute("description", "Whois lookup : Domain "+domain+".Use Our Free Whois Lookup Database To Search For & Reserve Your Domain Today at domain-infomation.appspot.com");
			req.setAttribute("keyword", domain+",whois,whois lookup,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info");
	        
	        try {
				req.getRequestDispatcher("/whois.jsp").forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        } else {
        	new domain.service.Whois(domain);
        	resp.sendRedirect("/whois/"+domain);
        }
    }
}
